package editor.project;

/**
 * Model for a java Package
 * 
 * @author Mark Dessain
 */
public class Package
{

	private Package parent = null;
	private ProjectFile file = null;
	
	/**
	 * @param file of the package
	 * @param parent of the package
	 */
	public Package(ProjectFile file, Package parent)
	{
		this.file = file;
		this.parent = parent;
	}
	
	/**
	 * Package with no file or parent set
	 */
	public Package()
	{
		this(null, null);
	}

	/**
	 * @return the package
	 */
	public Package getParent()
	{
		return parent;
	}
	
	/**
	 * @return link of packages list
	 */
	public String linkPackages()
	{
		String packageString = linkPackages(this);
		if(!packageString.equalsIgnoreCase(""))
		{
			return packageString.substring(1);// + ".";
		}
		else
		{
			return packageString;
		}
	}
	
	/**
	 * @param parent of the package
	 * @return
	 */
	private String linkPackages(Package parent)
	{
		String name = "";

		if(parent.getParent() != null)
		{
			name = linkPackages(parent.getParent()) + ".";
		}
		
		return name + parent.toString();
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString()
	{
		if(file == null)
		{
			return "";
		}
		else
		{
			return file.getName();
		}
	}
	
}
